<template>
  <div class="mod-log">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="用户名" clearable></el-input>
      </el-form-item>

      <el-form-item>
        <el-input v-model="dataForm.operator" placeholder="用户操作" clearable></el-input>
      </el-form-item>

      <el-form-item>
        <el-date-picker
          v-model="dataForm.begindate"
          type="datetime"
          placeholder="选择起始时间"
          align="right"
          default-time="00:00:00"
         format="yyyy-MM-dd HH:mm:ss" value-format="yyyy-MM-dd HH:mm:ss"  
          :picker-options="pickerOptions">
        </el-date-picker>
      </el-form-item>   

      <el-form-item>
        <el-date-picker
          v-model="dataForm.enddate"
          type="datetime"
          placeholder="选择结束时间"
          default-time="23:59:59"
          format="yyyy-MM-dd HH:mm:ss" value-format="yyyy-MM-dd HH:mm:ss" 
          align="right"
          :picker-options="pickerOptions">
        </el-date-picker>
      </el-form-item> 

      <el-form-item>
              <el-select
                    v-model="dataForm.orgid"
                    clearable
                    filterable
                    style="width:300px;"
                    remote
                
                    placeholder="输入机构代码或名称"
                    :remote-method="remoteMethod"
                    :loading="loading">
                    <el-option
                      v-for="item in list"
                      :key="item.branchid"
                      :label="item.branchname"
                      :value="item.branchid">
                    </el-option>
                  </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="getDataList()">查询</el-button>
        <el-button type="primary" @click="export2Excel()">导出</el-button>
        <el-button type="primary" v-print="printObj" v-if="false">打印</el-button>
      </el-form-item>
    </el-form>
    <el-table
      id = "table-detail"
      :data="dataList"
      border
      v-loading="dataListLoading"
      style="width: 100%">

      <el-table-column
        prop="username"
        header-align="center"
        align="center"
        label="用户名">
      </el-table-column>
      <el-table-column
        prop="operation"
        header-align="center"
        align="center"
        width="160px"
        label="用户操作">
      </el-table-column>

      <el-table-column
        prop="realname"
        header-align="center"
        align="center"
        width="120px"
        label="用户姓名">
      </el-table-column>

      <el-table-column
        prop="params"
        header-align="center"
        align="center"
        width="150"
        :show-overflow-tooltip="true"
        label="关键要素">
      </el-table-column>
      <el-table-column
        prop="time"
        header-align="center"
        width="110px"
        align="center"
        label="耗时(毫秒)">
      </el-table-column>
      <el-table-column
        prop="ip"
        header-align="center"
        align="center"
        width="150"
        label="IP地址">
      </el-table-column>

      <el-table-column
        prop="createDate"
        header-align="center"
        align="center"
        width="180"
        label="创建时间">
        </el-table-column>

      <el-table-column
        prop="orgid"
        header-align="center"
        align="center"
        width="300"
        label="发起机构">
      </el-table-column>
            <el-table-column
        prop="dsignuser"
        header-align="center"
        align="center"
                :show-overflow-tooltip="true"
        width="100"
        label="双签用户">
      </el-table-column>
      <el-table-column
        prop="depart"
        header-align="center"
        align="center"
        :show-overflow-tooltip="true"
        label="部门名称">
      </el-table-column>

      
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="datepicker.pageOptions"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
  </div>
</template>

<script>
  export default {
    data () {

      return {

        printLoading: true,
        printObj: {
          id: "table-detail",
          popTitle: '条形码打印',
          beforeOpenCallback (vue) {
            vue.printLoading = true
            console.log('打开之前')
          },
          openCallback (vue) {
            vue.printLoading = false
            console.log('执行了打印')
          },
          closeCallback (vue) {
            console.log('关闭了打印工具')
          }
        },

       pickerOptions: this.datepicker.pickerOptions,

        dataForm: {
          key: '',
          operator: '',
          begindate: '',
          enddate: '',
          orgid: ''
        },
        dataList: [],
        list: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        selectionDataList: []
      }
    },
    created () {
      this.init()
      this.getDataList()
    },
    methods: {

      export2Excel: function() {
            let xlsxParam = { raw: true };
            let tables = document.getElementById("table-detail");
            let table_book = this.$XLSX.utils.table_to_book(tables, xlsxParam);
            var table_write = this.$XLSX.write(table_book, {
              bookType: "xlsx",
              bookSST: true,
              type: "array"
            });
            try {
              this.$FileSaver.saveAs(
                new Blob([table_write], { type: "application/octet-stream" }),
                "业务量统计（按发起机构）.xlsx"
              );
            } catch (e) {
              if (typeof console !== "undefined") console.log(e, table_write);
            }
            return table_write;
        },

      remoteMethod(query) {

          this.loading = true;

          this.$http({
            url: this.$http.adornUrl(`/sys/user/branch/search`),
            method: 'get',
            params: this.$http.adornParams({
              "key": query || ''
            })
          }).then(({data}) => {
            this.loading = false;
            if (data && data.code === 0) {
                this.list = data.list;
            }
          })     
          

      },

      init(){
        this.remoteMethod()
      },

      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/sys/log/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'key': this.dataForm.key,
            'operator': this.dataForm.operator,
            'begindate': this.dataForm.begindate,
            'enddate': this.dataForm.enddate,
            'orgid': this.dataForm.orgid
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      }
    }
  }
</script>
